﻿#pragma execution_character_set("UTF-8")

#include "tcpclient.h"
#include <QFile>
#include <QDir>

TCPClient::TCPClient(QObject* parent)
    : QObject(parent)
{
    m_socket = new QTcpSocket(this);
}

void TCPClient::setHost(const QString& ip, const quint16& port)
{
    m_ip.clear();
    m_ip = QHostAddress(ip);
    m_port = port;
}

bool TCPClient::send_read_file_lists(const QString& dir)
{
    if (m_ip.isNull()) return false;

    m_socket->connectToHost(m_ip, m_port);
    if (m_socket->waitForConnected()) {
        QObject::disconnect(m_socket, nullptr, nullptr, nullptr);
        connect(m_socket, &QTcpSocket::readyRead, [&]() {
            QByteArray buf = m_socket->readAll();
            m_socket->disconnectFromHost();
            m_socket->close();
            emit receive_read_file_lists(QString(buf));
            buf.clear();
            });
        QString message = "read_file_lists##" + dir;
        m_socket->write(message.toLocal8Bit());
        return true;
    }

    return false;
}

bool TCPClient::send_copy_file(const QString& filename, const QString& sourceDir, const QString& destDir)
{
    if (m_ip.isNull()) return false;

    m_filename = filename;
    m_sourceDir = sourceDir;
    m_destDir = destDir;
    m_recvSize = 0;
    m_fileSize = 0;
    m_socket->connectToHost(m_ip, m_port);
    if (m_socket->waitForConnected()) {
        QObject::disconnect(m_socket, nullptr, nullptr, nullptr);
        connect(m_socket, &QTcpSocket::readyRead, [&]() {
            QByteArray buf = m_socket->readAll();
            {
                bool ok = false;
                auto size = buf.toLongLong(&ok);
                buf.clear();

                m_socket->disconnectFromHost();
                m_socket->close();

                if (ok) {
                    m_fileSize = size;
                    send_copy_file_inter();
                }
                else {
                    emit receive_copy_file(false);
                }
            }
            });

        QString message = "read_file_infos##" + sourceDir + "/" + filename;
        m_socket->write(message.toLocal8Bit());
        return true;
    }

    return false;
}

bool TCPClient::send_copy_file_inter()
{
    if (m_ip.isNull()) return false;

    m_recvSize = 0;
    m_socket->connectToHost(m_ip, m_port);
    if (m_socket->waitForConnected()) {
        QObject::disconnect(m_socket, nullptr, nullptr, nullptr);
        QDir dir(m_destDir);
        if (!dir.exists()) {
            dir.mkpath(dir.absolutePath());
        }

        m_filebuf.clear();
        connect(m_socket, &QTcpSocket::readyRead, [&]() {
            QByteArray buf = m_socket->readAll();
            m_filebuf.append(buf);
            m_recvSize += buf.size();
            buf.clear();
            if (m_recvSize >= m_fileSize) {
                QFile file(m_destDir + "/" + m_filename);
                file.open(QIODevice::WriteOnly);
                file.write(m_filebuf);
                file.close();
                m_filebuf.clear();
                m_socket->disconnectFromHost();
                m_socket->close();
                emit receive_copy_file(true);
            }
            });

        QString message = "copy_file##" + m_sourceDir + "/" + m_filename;
        m_socket->write(message.toLocal8Bit());
        return true;
    }

    return false;
}
